package com.xdf.xzymanagementsystem.service.impl;

import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.xdf.xzymanagementsystem.dao.OrderFeeMapper;
import com.xdf.xzymanagementsystem.entity.OrderDetail;
import com.xdf.xzymanagementsystem.entity.OrderFee;
import com.xdf.xzymanagementsystem.service.OrderFeeService;
import com.xdf.xzymanagementsystem.util.SystemUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Description : 服务层实现类，由代码生成器自动生成
 * @Author : chanchaw
 * @Date : 2025-3-22 10:27:04
 */
@Slf4j
@Service
public class OrderFeeServiceImpl extends ServiceImpl<OrderFeeMapper, OrderFee> implements OrderFeeService {
    @Autowired
    private OrderFeeMapper dao;
    @Autowired
    private SystemUtils systemUtils;
    //=================以上是自动生成的代码，自定义代码请写在下面==================

    /**
     * 新增单据
     * @param record
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public OrderFee insert(OrderFee record) {
        record.setCreateUser(systemUtils.getLoginUsername());
        record.setState(1);
        dao.insertSelective(record);
        OrderFee orderFee = dao.selectOneById(record.getId());
        if (orderFee == null) throw new RuntimeException("新增失败，请重试！");
        return orderFee;
    }

    @Override
    public List<OrderFee> selectByMainId(Integer id) {
        List<OrderFee> list = dao.selectByParentId(id);
        return list;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public OrderFee update(OrderFee record) {
        record.setLastTime(new Date());
        record.setLastUser(systemUtils.getLoginUsername());
        dao.update(record);
        OrderFee orderFee = dao.selectOneById(record.getId());
        return orderFee;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteById(Integer id) {
        OrderFee orderFee = dao.selectOneById(id);
        orderFee.setIsDelete(1);
        orderFee.setLastTime(new Date());
        orderFee.setLastUser(systemUtils.getLoginUsername());
        dao.update(orderFee);
    }

    @Override
    public List<Map<String, Object>> selectAllByDate(Date sdate, Date edate) {
        return dao.selectAllByDate(sdate, edate);
    }
}